package crack;

import java.io.IOException;
import java.util.ArrayList;

import fem2.Constraint;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.enu.State;
import fem2.material.StVenantKirchhoffMM;
import fem2.observer.ResistantGraphicalObserver;
import fem2.observer.TimeStepObserver;
import fem2.strategies.Strategy;

public abstract class AbstractLshapeWinklerUsingSDAElementDemo extends
		AbstractLshapeUsingSDAElementDemo {

	public AbstractLshapeWinklerUsingSDAElementDemo() {
		projectName = "LshapeWinkler.gid";
	}

	@Override
	public Model createConditions(Model m) {
		Mesh mesh = m.getMesh();
		Constraint c1 = new Constraint(false, false);
		Constraint c2 = new Constraint(true, false);
		double du = -0.005;
		c2.setValue(1, du);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 0, 1, -500));
		MeshUtilities.seekNode(mesh, 500, 250).setConstraint(c2);
		return m;
	}

	@Override
	public MaterialModel createMaterial(Model m) {
		double E = 25850;
		double nu = 0.18;
		double t = 100;
		State ss = State.PLANE_STRESS;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);
		return mm;
	}

	@Override
	public void addObservers(Model m, Strategy s, TimeStepObserver o) {
		super.addObservers(m, s, o);

		ArrayList<Node> top = MeshUtilities.seekNodesOnLine(m.getMesh(), 0, 500, 1, 0);
		Node[] nodes = new Node[top.size()];
		for (int i = 0; i < nodes.length; i++) {
			nodes[i] = top.get(i);
		}

		ResistantGraphicalObserver ro = new ResistantGraphicalObserver(m, s, 2, nodes);
		ro.setInverse(true);
		ro.addOrigin();
		try {
			String fn = projectDir + '/' + projectName + '/' + meshFileName + ".ldcurve.txt";
			ro.setOutputToFile(fn);
		} catch (IOException e) {
			e.printStackTrace();
			throw new Error("Write failed");
		}

		s.addObserver(ro);
	}
}
